package controller;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import entity.User;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import vo.AjaxResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2023-08-14 16:05
 * Description：<描述>
 */
@RestController
public class ParamController{

    @SneakyThrows
    @RequestMapping("/param1")
    public void param1(HttpServletRequest req, HttpServletResponse res, HttpSession session){
        System.out.println(req.getParameter("name"));
        System.out.println(req.getHeader("User-Agent"));
        System.out.println(req.getServletContext().getRealPath("upload"));

        System.out.println("会话id:"+session.getId());

        res.setContentType("application/json;charset=utf8");
        res.getWriter().write("ok");
    }

    @RequestMapping("/param2")
    public AjaxResult param2(@RequestParam("username") String name, @RequestParam(defaultValue = "10", required = false) int age, Double height){
        System.out.println(name);
        System.out.println(age);
        System.out.println(height);
        return AjaxResult.success();
    }

    @RequestMapping("/param3/{uid}/{name}")
    public AjaxResult param3(@PathVariable("uid") int id, @PathVariable String name){
        System.out.println(id);
        System.out.println(name);
        return AjaxResult.success();
    }

    @RequestMapping("/param4")
    public AjaxResult param4(@RequestHeader("User-Agent") String userAgent){
        System.out.println(userAgent);
        UserAgent agent = UserAgentUtil.parse(userAgent);
        System.out.println("操作系统："+agent.getOs());
        System.out.println("浏览器："+agent.getBrowser().getName());
        return AjaxResult.success();
    }

    @RequestMapping("/param5")
    public AjaxResult param5(@CookieValue("username") String username){
        System.out.println(username);
        return AjaxResult.success();
    }

    @RequestMapping("/param6")
    public AjaxResult param6(@RequestBody User user){
    // public AjaxResult param6(@RequestBody Integer[] ids){
    // public AjaxResult param6(@RequestBody List<Integer> ids){
    // public AjaxResult param6(@RequestBody String str){
        System.out.println(user);
        // System.out.println(Arrays.toString(ids));
        // System.out.println(ids);
        // System.out.println(str);
        // JSONObject obj = JSONUtil.parseObj(str);
        // System.out.println(obj.get("uid"));

        return AjaxResult.success();
    }

    @RequestMapping("/param7")
    public AjaxResult param7(@ModelAttribute User user){
        System.out.println(user);
        return AjaxResult.success();
    }

}
